<?php
/**
 * @author spenceryg
 * @date 2022/11/26
 * @time 10:13 下午
 * @focus love & life
 */


namespace app\auto\model;


use app\auto\lib\ControllerGenerator;
use think\helper\Str;
use think\Model;

class Table extends \think\Model
{
    protected $table = 'auto_table';

   public static function onAfterDelete(Model $model): void
   {
       $table_id = $model->getAttr('id');

       Controller::destroy(function ($query) use ( $model){
          $query->where('name', Str::studly(str_replace(env('DATABASE.PREFIX'),'', $model->getAttr('name'))));
       });

       Field::destroy(function ($query) use ($table_id) {
           $query->where('table_id', $table_id);
       });


   }


    /**
     * iField
     * @return \think\model\relation\HasMany
     */
    public function iField(): \think\model\relation\HasMany
    {
        return $this->hasMany(Field::class);
   }
}
